package pl.us.ij.plugins.segmentation.forms;

import com.intellij.uiDesigner.core.Spacer;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import pl.us.ij.plugins.segmentation.commands.filters.AveragedFilterCommand;
import pl.us.ij.plugins.segmentation.commands.filters.FilterCommandParams;
import pl.us.ij.plugins.segmentation.infrastructure.CategoryInfo;
import pl.us.ij.plugins.segmentation.infrastructure.CommandParams;
import pl.us.ij.plugins.segmentation.localization.Localization;

import javax.swing.*;
import java.util.ResourceBundle;

/**
 * Created by IntelliJ IDEA.
 * User: Przemek
 * Date: 2010-05-25
 * Time: 23:47:40
 * To change this template use File | Settings | File Templates.
 */
public class FilterAveragedForm extends CategoryInfo {
    private JPanel averagedPanel;
    private JSpinner averagedIterationCountSpinner;
    private JButton averagedApplyButton;

    public FilterAveragedForm(IActionHandler actionHandler) {
        averagedIterationCountSpinner.setModel(new SpinnerNumberModel(1, 1, 10, 1));

        actionHandler.setAction(averagedApplyButton, AveragedFilterCommand.class, getAveragedFilterParams());
    }

    @Override
    public String getTitle() {
        return Localization.getString("filterAveragedForm.title");
    }

    @Override
    public JPanel getPanel() {
        return averagedPanel;
    }

    @Override
    public String getDescription() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    private ICommandParamGather getAveragedFilterParams() {
        return new ICommandParamGather() {
            public CommandParams getCommandParams() {
                FilterCommandParams params = new FilterCommandParams();
                params.setIterationCount(((Number) averagedIterationCountSpinner.getValue()).intValue());
                return params;
            }
        };
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new FormLayout("fill:d:noGrow", "center:d:noGrow"));
        averagedPanel = new JPanel();
        averagedPanel.setLayout(new FormLayout("fill:d:noGrow", "center:d:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:d:grow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        CellConstraints cc = new CellConstraints();
        panel1.add(averagedPanel, cc.xy(1, 1, CellConstraints.LEFT, CellConstraints.FILL));
        final JLabel label1 = new JLabel();
        this.$$$loadLabelText$$$(label1, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("filterAveragedForm.iterationCountLabel"));
        averagedPanel.add(label1, cc.xy(1, 1));
        averagedIterationCountSpinner = new JSpinner();
        averagedPanel.add(averagedIterationCountSpinner, cc.xy(1, 3, CellConstraints.FILL, CellConstraints.DEFAULT));
        final com.intellij.uiDesigner.core.Spacer spacer1 = new com.intellij.uiDesigner.core.Spacer();
        averagedPanel.add(spacer1, cc.xy(1, 5, CellConstraints.DEFAULT, CellConstraints.FILL));
        averagedApplyButton = new JButton();
        averagedApplyButton.setHideActionText(false);
        this.$$$loadButtonText$$$(averagedApplyButton, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("filterAveragedForm.filterLabel"));
        averagedApplyButton.setVerticalAlignment(0);
        averagedPanel.add(averagedApplyButton, cc.xy(1, 7, CellConstraints.FILL, CellConstraints.BOTTOM));
        label1.setLabelFor(averagedIterationCountSpinner);
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadLabelText$$$(JLabel component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setDisplayedMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadButtonText$$$(AbstractButton component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }
}
